﻿using System.Collections.Generic;

namespace DevOnServer
{
    public enum AccessLevel
    {
        Banned,
        Player = 70,
        GameMaster = 76,
        Admin = 80
    }

    public class CAccount
    {
        public int ID;

        public string Username;

        public string Password;

        public int AccessLevel;

        public int BanTime;

        public bool Connected;
    }

    public partial class CDatabase
    {
        private Dictionary<string, CAccount> m_accounts = new Dictionary<string, CAccount>();

        public CAccount getAccount(string username)
        {
            username = username.ToLower();

            if (m_accounts.ContainsKey(username))
                return m_accounts[username];

            CQueryResult result = m_dbc.result(
                "SELECT id, password, accesslevel, bantime FROM accounts WHERE username='{0}' LIMIT 1;",
                username);

            if (result != null && result.advance())
            {
                CAccount account = new CAccount();
                account.Username = username;
                account.ID = result.read<int>("id");
                account.Password = result.read<string>("password");
                account.AccessLevel = result.read<int>("accesslevel");
                account.BanTime = result.read<int>("bantime");

                result.Dispose();

                m_accounts[username] = account;

                return account;
            }

            result.Dispose();

            return null;
        }
    }
}